//
//  XSFileManager.m
//  AudioVideo
//
//  Created by xueshan1 on 2019/8/22.
//  Copyright © 2019 xueshan1. All rights reserved.
//

#import "XSFileManager.h"

@implementation XSFileManager

+ (instancetype)shareInstance{
    static XSFileManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[[self class]alloc]init];
    });
    return manager;
}

+ (NSString *)documentPath{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docPath = [paths firstObject];
    return docPath;
}
+ (NSString *)tempPath{
    return NSTemporaryDirectory();
}

+ (BOOL)createDirectoryInDocumentWithPathComponent:(NSString *)pathComponent{
    NSFileManager *manager = [NSFileManager defaultManager];
    NSString *path = [[self documentPath]stringByAppendingPathComponent:pathComponent];
    if ([manager fileExistsAtPath:path]) {
        [manager removeItemAtPath:path error:nil];
    }
    BOOL isOK = [manager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
    return isOK;
}
+ (BOOL)removeDirectoryInDocWithPathComponent:(NSString *)pathComponent{
    NSFileManager *manager = [NSFileManager defaultManager];
    NSString *path = [[self documentPath]stringByAppendingPathComponent:pathComponent];
    if ([manager fileExistsAtPath:path]) {
        return [manager removeItemAtPath:path error:nil];
    }
    return NO;
}

+ (void)deletePath:(NSString *)path{
    if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
        BOOL isOK = [[NSFileManager defaultManager]removeItemAtPath:path error:nil];
        if (isOK) {
//            NSLog(@"delete suc");
        }else{
            NSLog(@"delete fail");
        }
    }else{
        NSLog(@"no exists delete file");
    }
}

+ (NSString *)tempFilePathWithPathComponent:(NSString *)pathComponent{
   return [[self tempPath]stringByAppendingPathComponent:pathComponent];
}

+ (void)deleteTempFileWithPathComponent:(NSString *)pathComponent{
    NSString *path = [[self tempPath]stringByAppendingPathComponent:pathComponent];
    NSFileManager *fm = [NSFileManager defaultManager];
    NSError *err;
    if ([fm fileExistsAtPath:path]) {
        [fm removeItemAtPath:path error:nil];
        NSLog(@"file deleted");
        if (err) {
            NSLog(@"file deleted error, %@", err.localizedDescription );
        }
    }
    
}

+ (void)clearTemp{
    NSArray *subPaths = [[NSFileManager defaultManager]subpathsAtPath:[self tempPath]];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        for (NSString *path in subPaths) {
            [self deletePath:path];
        }
    });
    
}
+ (NSString *)tempVideoDirectory{
    NSString *videoDir = [[self tempPath] stringByAppendingPathComponent:@"xsTempVideo"];
    NSFileManager *fm = [NSFileManager defaultManager];
    if (![fm fileExistsAtPath:videoDir]){
        [fm createDirectoryAtPath:videoDir withIntermediateDirectories:YES attributes:nil error:nil];
    }
    return videoDir;
}
+ (NSString *)tempVideoPath{
    NSDateFormatter *fm = [NSDateFormatter new];
    fm.dateFormat = @"MM-dd-HH:mm:ss";
    return [[self tempVideoDirectory] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.MOV",[fm stringFromDate:[NSDate date]]]];
}
+ (NSURL *)tempVideoURL{
    NSDateFormatter *fm = [NSDateFormatter new];
    fm.dateFormat = @"MM-dd-HH:mm:ss";
    NSString *path = [[self tempVideoDirectory] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.MOV",[fm stringFromDate:[NSDate date]]]];
    return [NSURL fileURLWithPath:path];
}





@end
